home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 2
/
Atari Mega Archive CD - Volume 2.iso
/
minix
/
up1510b.tgz
/
up1510b
/
src
/
commands
/
readfs.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-07-23
|
15KB
|
580 lines
/* readfs - read a MINIX file system Author: Paul Polderman */
/* Command: readfs - read and extract a MINIX filesystem.
*
* Syntax: readfs [-li] block-special [directory]
*
* Flags: -l: Extract files and dirs and produce a mkfs-listing on stdout
* -i: Information only: give the listing, but do not extract files.
*
* Examples: readfs /dev/fd1 # extract all files from /dev/fd1.
* readfs -i /dev/hd2 # see what's on /dev/hd2.
* readfs -l /dev/at0 rootfs # extract and list the filesystem
* # of /dev/at0 and put the tree
* # in the directory `rootfs'.
*
* Readfs reads a MINIX filesystem and extracts recursively all directories
* and files, and (optionally) produces a mkfs-listing of them on stdout.
* The root directory contents are placed in the current directory, unless
* a directory is given as argument, in which case the contents are put there.
* Readfs tries to restore the attributes (mode/uid/gid/time) of the files
* extracted to those of the original files.
* Special files are created as ordinary files, but the mkfs-listing
* enables mkfs to restore them to original.
*/
#include <sys/types.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include <stdio.h>
#include <minix/config.h>
#include <minix/const.h>
#include <minix/type.h>
#include "../fs/const.h"
#include "../fs/type.h"
#include "../fs/buf.h"
#include "../fs/super.h"
#undef printf /* Definition used only in the kernel */
#define block_nr long /* To allow big filesystems even if FS
* doesn't. */
/* Compile with -I/user0/ast/minix
* (i.e. the directory containing the MINIX system sources)
*
* Author: Paul Polderman (polder@cs.vu.nl) April 1987
*/
char verbose = 0; /* give a mkfs-listing of the filesystem */
/* And extracts its contents. */
char noaction = 0; /* just give a mkfs-listing, do not extract
* files. */
struct super_block sb;
char pathname[1024];
main(argc, argv)
int argc;
char **argv;
{
switch (argc) {
case 2:
pathname[0] = '\0';
readfs(argv[1], pathname);
break;
case 3:
if (argv[1][0] == '-') {
get_flags(&argv[1][1]);
pathname[0] = '\0';
readfs(argv[2], pathname);
} else {
strcpy(pathname, argv[2]);
readfs(argv[1], pathname);
}
break;
case 4:
if (argv[1][0] == '-') {
get_flags(&argv[1][1]);
strcpy(pathname, argv[3]);
readfs(argv[2], pathname);
break;
} /* else fall through .. */
default:
fprintf(stderr, "Usage: %s [-li] <special> [dirname]\n", argv[0]);
exit(1);
}
exit(0);
}
get_flags(flags)
register char *flags;
{
while (*flags) {
switch (*flags) {
case 'L':
case 'l': verbose = 1; break;
case 'I':
case 'i':
noaction = 1;
verbose = 1;
break;
default:
fprintf(stderr, "Bad flag: %c\n", *flags);
break;
}
flags++;
}
}
#define zone_shift (sb.s_log_zone_size) /* zone to block ratio */
readfs(special_file, directory)
char *special_file, *directory;
/* Readfs: opens the given special file (with MINIX filesystem),
* and extracts its contents into the given directory.
*/
{
d_inode root_inode;
int special;
off_t super_b;
umask(0);
/* Open the special file */
if ((special = open(special_file, O_RDONLY)) < 0) {
fprintf(stderr, "cannot open %s\n", special_file);
return;
}
/* Read the superblock */
super_b = (off_t) SUPER_BLOCK *(off_t) BLOCK_SIZE;
if (lseek(special, super_b, SEEK_SET) != super_b) {
fprintf(stderr, "cannot seek to superblock\n");
return;
}
if (read(special, (char *) &sb, sizeof(struct super_block))
!= sizeof(struct super_block)) {
fprintf(stderr, "cannot read superblock\n");
return;
}
/* Is it really a MINIX filesystem ? */
if (sb.s_magic != SUPER_MAGIC) {
fprintf(stderr, "%s is not a valid MINIX filesystem\n",
special_file);
return;
}
/* Fetch the inode of the root directory */
if (get_inode(special, (ino_t) ROOT_INODE, &root_inode) < 0) {
fprintf(stderr, "cannot get inode of root directory\n");
return;
}
/* Print number of blocks and inodes */
if (verbose) printf("boot\n%ld %d\n",
(block_nr) sb.s_nzones << zone_shift, sb.s_ninodes);
/* Extract (recursively) the root directory */
dump_dir(special, &root_inode, directory);
}
/* Different type of blocks: (used in routine get_block for caching) */
#define B_INODE 0 /* Cache #0 is the inode cache */
#define B_INDIRECT 1 /* Cache #1 is the (dbl) indirect block cache */
#define B_DATA 2 /* No cache for data blocks (only read once) */
int get_inode(fd, inum, ip)
int fd;
ino_t inum;
d_inode *ip;
/* Get inode `inum' from the MINIX filesystem. (Uses the inode-cache) */
{
struct buf bp;
block_nr block;
block_nr ino_block;
unshort ino_offset;
int r;
/* Calculate start of i-list */
block = SUPER_BLOCK + 1 + sb.s_imap_blocks + sb.s_zmap_blocks;
/* Calculate block with inode inum */
ino_block = ((inum - 1) / INODES_PER_BLOCK);
ino_offset = ((inum - 1) % INODES_PER_BLOCK);
block += ino_block;
/* Fetch the block */
if (get_block(fd, block, &bp, B_INODE) == 0) {
bcopy(&bp.b_inode[ino_offset], ip, sizeof(d_inode));
return(0);
}
/* Oeps, foutje .. */
fprintf(stderr, "cannot find inode %d\n", inum);
return(-1);
}
static int indent = 0; /* current indent (used for mkfs-listing) */
dump_dir(special, ip, directory)
int special;
d_inode *ip;
char *directory;
/* Make the given directory (if non-NULL),
* and recursively extract its contents.
*/
{
register dir_struct *dp;
register int n_entries;
register char *name;
block_nr b = 0;
d_inode dip;
struct buf bp;
if (verbose) {
show_info(directory, ip, "");
indent++;
}
if (!noaction && *directory) {
/* Try to make the directory if not already there */
if (mkdir(directory) != 0 || chdir(directory) < 0) {
fprintf(stderr, "mkdir %s failed\n", directory);
return;
}
}
for (name = directory; *name; name++) /* Find end of pathname */
;
*name++ = '/'; /* Add trailing slash */
n_entries = (int) (ip->i_size / (off_t) sizeof(dir_struct));
while (n_entries > 0) {
/* Read next block of the directory */
if (get_fileblock(special, ip, b, &bp) < 0) return;
dp = &bp.b_dir[0];
if (b++ == (block_nr) 0) {
dp += 2; /* Skip "." and ".." */
n_entries -= 2;
}
/* Extract the files/directories listed in the block */
while (n_entries-- > 0 && dp < &bp.b_dir[NR_DIR_ENTRIES]) {
if (dp->d_inum != (ino_t) 0) {
if (get_inode(special, dp->d_inum, &dip) < 0) {
/* Bad luck */
dp++;
continue;
}
/* Add new pathname-component to `pathname'. */
strncpy(name, dp->d_name, NAME_MAX);
name[NAME_MAX] = '\0';
/* Call the right routine */
if ((dip.i_mode & I_TYPE) == I_DIRECTORY)
dump_dir(special, &dip, name);
else
dump_file(special, &dip, name);
}
dp++; /* Next entry, please. */
}
}
*--name = '\0'; /* Restore `pathname' to what it was. */
if (!noaction && *directory) {
chdir(".."); /* Go back up. */
restore(directory, ip); /* Restore mode/owner/accesstime */
}
if (verbose) {
do_indent(--indent); /* Let mkfs know we are done */
printf("$\n"); /* with this directory. */
}
}
dump_file(special, ip, filename)
int special;
d_inode *ip;
char *filename;
/* Extract given filename from the MINIX-filesystem,
* and store it on the local filesystem.
*/
{
dir_struct *dp;
int file;
block_nr b = 0;
struct buf bp;
off_t size;
if (verbose) show_info(filename, ip, pathname);
if (noaction) return(0);
if (access(filename, 0) == 0) {
/* Should not happen, but just in case .. */
fprintf(stderr, "Will not create %s: file exists\n", filename);
return(-1);
}
if ((file = creat(filename, (ip->i_mode & ALL_MODES))) < 0) {
fprintf(stderr, "cannot create %s\n", filename);
return(-1);
}
/* Don't try to extract /dev/hd0 */
if ((ip->i_mode & I_TYPE) == I_REGULAR) {
size = ip->i_size;
while (size > (off_t) 0) {
/* Get next block of file */
if (get_fileblock(special, ip, b++, &bp) < 0) {
close(file);
return(-1);
}
/* Write it to the file */
if (size > (off_t) BLOCK_SIZE)
write(file, bp.b_data, BLOCK_SIZE);
else
write(file, bp.b_data, (int) size);
size -= (off